package com.xywy.fwc;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.PopupWindow.OnDismissListener;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.xywy.R;
import com.xywy.common.BaseActivity;
import com.xywy.common.util.MD5Util;
import com.xywy.common.widget.zrcListView.SimpleFooter;
import com.xywy.common.widget.zrcListView.SimpleHeader;
import com.xywy.common.widget.zrcListView.ZrcListView;
import com.xywy.common.widget.zrcListView.ZrcListView.OnItemClickListener;
import com.xywy.common.widget.zrcListView.ZrcListView.OnStartListener;
import com.xywy.fwc.adapter.DoctorAdapter;
import com.xywy.fwc.bean.DoctorBean;
import com.xywy.fwc.bean.DoctorBeanInfo;
import com.xywy.fwc.net.DataCallBack;
import com.xywy.fwc.net.DoctorBeanInfoParser;
import com.xywy.fwc.net.Request;
import com.xywy.fwc.widget.FwcPopWindow;
import com.xywy.me.bean.UserKeeper;
/**
 * 家庭医生
 * @author wwl
 *
 */
public class FwcHomeDoctor extends BaseActivity implements OnClickListener{
	private TextView tv_week_look;
	private int popup_status = 0;
	public static final int POPUP_OPEN = 1;
	public static final int POPUP_CLOSE = 0;
	private Drawable drawable_up;
	private Drawable drawable_down;
	private String userid;
	private SharedPreferences sp;
	private int currentPage = 1;
	private String temp_url = "http://api.3g.club.xywy.com/familydoctor.php?type=user_orders&pagesize=2&page=";
    
	private ZrcListView zrclist_view;
	private DoctorAdapter adapter;
	private List<DoctorBean> doctorBeans = new ArrayList<DoctorBean>();
	
	
	private LinearLayout lin_process;
	private ProgressBar process;
	private TextView tv_reload;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fwc_home);
		initView();
		
		sp = getSharedPreferences("config", Context.MODE_PRIVATE);
//		userid = sp.getString("userid", "");
		userid = UserKeeper.getCurrentUser(this).getUserid();
		
//		userid = 15963569+"";
		loadData();
	}
	@Override
	public void preInit() {

	}

	@Override
	public void loadData() {
		try {
			String sign = MD5Util.md5("user_orders"+FwcConstant.APPKEY);
		    String tempurl = temp_url+currentPage+
		    		"&uid="+userid+"&sign="+sign
		    		;
//			String url = FwcConstant.URL_HOME_DOCTOR+"&uid="+userid+"&sign="+MD5Util.md5("user_orders"+FwcConstant.APPKEY);
			System.out.println(temp_url);
			Request.requestData(new DoctorBeanInfoParser(), call, tempurl);
		} catch (Exception e) {
			e.printStackTrace();
		}
	   
	}
	DataCallBack<DoctorBeanInfo> call = new DataCallBack<DoctorBeanInfo>() {

		@Override
		public void dataCall(DoctorBeanInfo docinfo) {
			if(docinfo != null){
				if(docinfo.getDoctorbeans() != null){
					if(currentPage ==1){
						zrclist_view.setRefreshSuccess("加载成功"); // 通知加载成功
						zrclist_view.startLoadMore(); // 开启LoadingMore功能
						doctorBeans.clear();
					}else {
						zrclist_view.setLoadMoreSuccess();
					}

					doctorBeans.addAll(docinfo.getDoctorbeans());
					System.out.println("------------00--"+doctorBeans.size());
					runOnUiThread(new Runnable() {
						public void run() {
							adapter.setList(doctorBeans);
							if(doctorBeans != null && doctorBeans.size()>0){
								loadSuccess();
							}else {
								loadNoData();
							}
						}
					});
				}
			}else {
				runOnUiThread(new Runnable() {
					public void run() {
						loadError();
					}
				});
			
			}
		}
	};

	@Override
	public void initView() {
		zrclist_view = (ZrcListView) findViewById(R.id.zrclist);
		findViewById(R.id.tv_title_back).setOnClickListener(this);
		findViewById(R.id.bt_title_back).setOnClickListener(this);
		adapter = new DoctorAdapter(doctorBeans, this);
		zrclist_view.setAdapter(adapter);
		
		lin_process = (LinearLayout) findViewById(R.id.lin_progress);
    	process = (ProgressBar) findViewById(R.id.process);
    	tv_reload = (TextView) findViewById(R.id.tv_reload);
    	tv_reload.setOnClickListener(this);
		lvOption();
	}
	@Override
	public void onClick(View view) {
         switch (view.getId()) {
		case R.id.tv_title_back:
		case R.id.bt_title_back:
		    finish();
			break;
		case R.id.tv_reload:
			if(tv_reload.getText().toString().equals("加载失败")){
				process.setVisibility(View.VISIBLE);
				tv_reload.setText("正在加载");
			}
			break;
		default:
			break;
		}		
	}
	
	 public void loadError(){
	    	if(doctorBeans == null || doctorBeans.size()==0){
	    		zrclist_view.setVisibility(View.GONE);
	    		lin_process.setVisibility(View.VISIBLE);
	    		tv_reload.setText("加载失败");
	    		process.setVisibility(View.GONE);
	    	}
	    }
	    
	 public void loadSuccess(){
	    	zrclist_view.setVisibility(View.VISIBLE);
			lin_process.setVisibility(View.GONE);
	    }
	 public void loadNoData(){
		 zrclist_view.setVisibility(View.GONE);
		lin_process.setVisibility(View.VISIBLE);
		tv_reload.setText("没有数据");
		process.setVisibility(View.GONE);
	 }
	
	
	
	private void lvOption() {
		// 设置默认偏移量，主要用于实现透明标题栏功能。（可选）
		float density = getResources().getDisplayMetrics().density;
		// lv.setFirstTopOffset((int) (0 * density));

		// 设置下拉刷新的样式（可选，但如果没有Header则无法下拉刷新）
		SimpleHeader header = new SimpleHeader(this);
		header.setTextColor(0xff666666);
		header.setCircleColor(0xff1fb5ad);
		zrclist_view.setHeadable(header);

		// 设置加载更多的样式（可选）
		SimpleFooter footer = new SimpleFooter(this);
		footer.setCircleColor(0xff1fb5ad);
		zrclist_view.setFootable(footer);

		// 设置列表项出现动画（可选）
		zrclist_view.setItemAnimForTopIn(R.anim.topitem_in);
		zrclist_view.setItemAnimForBottomIn(R.anim.bottomitem_in);

		// 下拉刷新事件回调（可选）
		zrclist_view.setOnRefreshStartListener(new OnStartListener() {
			@Override
			public void onStart() {
				currentPage = 1;
				loadData();
//				refresh();
			}
		});

		// 加载更多事件回调（可选）
		zrclist_view.setOnLoadMoreStartListener(new OnStartListener() {
			@Override
			public void onStart() {
				currentPage++;
//				loadMore();
				loadData();
			}
		});
//		lv.refresh(); // 主动下拉刷新

	}
	
	
	

}
